package com.example.elm.Account

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.example.elm.Public.AccountInfo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import com.example.elm.Public.AddressActivity
import com.example.elm.databinding.ProfileMainBinding

private var AccountInformation = AccountInfo("", "","","","","")
class AccountMainFragment:Fragment() {
    private lateinit var binding: ProfileMainBinding
    private val loginLauncher = registerForActivityResult(
        LoginContract()){
            result -> // result为回调函数的返回值
        Toast.makeText(requireContext(), result.account, Toast.LENGTH_SHORT).show()
        if(result.account.isNotEmpty()){
            AccountInformation = result
            saveAccountInformation(result)
            updateUI(true)
        }
    }
    private val registerLauncher = registerForActivityResult(
        RegisterContract()){
            result -> // result为回调函数的返回值
    }
    private val settingLauncher = registerForActivityResult(
        SettingContract()){
            result -> // result为回调函数的返回值
        if(result.account == ""){
            AccountInformation = result
            clearAccountInformation()
            updateUI(false)
        }
        else{
            AccountInformation = result
            saveAccountInformation(result)
            updateUI(true)
        }
    }
    private val foodcardLauncher = registerForActivityResult(
        FoodcardContract()){
            result -> // result为回调函数的返回值
    }
    private val eatbeanLauncher = registerForActivityResult(
        EatbeanContract()){
            result -> // result为回调函数的返回值
    }
    private val hongbaoLauncher = registerForActivityResult(
        HongbaoContract()){
            result -> // result为回调函数的返回值
    }
    private val odersLauncher = registerForActivityResult(
        OdersContract()){
            result -> // result为回调函数的返回值
    }
    private val followstoreLauncher = registerForActivityResult(
        FollowstoreContract()){
            result -> // result为回调函数的返回值
    }
    private val ruleLauncher = registerForActivityResult(
        RuleContract()){
            result -> // result为回调函数的返回值
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = ProfileMainBinding.inflate(inflater,container,false)
        // 从 SharedPreferences 加载账户信息
        loadAccountInformation()

        binding.login.setOnClickListener {
            loginLauncher.launch(AccountInformation)
        }
        binding.register.setOnClickListener {
            registerLauncher.launch(AccountInformation)
        }
        binding.setting.setOnClickListener {
            settingLauncher.launch(AccountInformation)
        }
        binding.foodcard.setOnClickListener {
            foodcardLauncher.launch(AccountInformation)
        }
        binding.eatbean.setOnClickListener {
            eatbeanLauncher.launch(AccountInformation)
        }
        binding.redpacket.setOnClickListener {
            hongbaoLauncher.launch(AccountInformation)
        }
        binding.all.setOnClickListener {
            odersLauncher.launch(AccountInformation)
        }
        binding.store.setOnClickListener {
            followstoreLauncher.launch(AccountInformation)
        }
        binding.rule.setOnClickListener {
            ruleLauncher.launch(AccountInformation)
        }
        binding.address.setOnClickListener {
            val intent = Intent(requireContext(), AddressActivity::class.java)
            startActivity(intent)
        }
        return binding.root
    }

    private fun loadAccountInformation() {
        val sharedPreferences = requireContext().getSharedPreferences("AccountPrefs", Context.MODE_PRIVATE)
        val account = sharedPreferences.getString("account", "") ?: ""
        val password = sharedPreferences.getString("password", "") ?: ""
        val name = sharedPreferences.getString("name", "") ?: ""
        val peopleName = sharedPreferences.getString("people_name", "") ?: ""
        val headImage = sharedPreferences.getString("head_image", "") ?: ""
        val introduction = sharedPreferences.getString("introduction", "") ?: ""

        if (account.isNotEmpty()) {
            AccountInformation = AccountInfo(account, password, name, peopleName, headImage, introduction)
            updateUI(true)
        } else {
            updateUI(false)
        }
    }

    private fun saveAccountInformation(accountInfo: AccountInfo) {
        val sharedPreferences = requireContext().getSharedPreferences("AccountPrefs", Context.MODE_PRIVATE)
        with(sharedPreferences.edit()) {
            putString("account", accountInfo.account)
            putString("password", accountInfo.password)
            putString("name", accountInfo.name)
            putString("people_name", accountInfo.people_name)
            putString("head_image", accountInfo.head_image)
            putString("introduction", accountInfo.introduction)
            apply()
        }
    }

    private fun clearAccountInformation() {
        val sharedPreferences = requireContext().getSharedPreferences("AccountPrefs", Context.MODE_PRIVATE)
        with(sharedPreferences.edit()) {
            clear()
            apply()
        }
    }

    private fun updateUI(isLoggedIn: Boolean) {
        binding.login.isGone = isLoggedIn
        binding.register.isGone = isLoggedIn
        binding.divider.isGone = isLoggedIn
        binding.head.isGone = !isLoggedIn
        binding.name.isGone = !isLoggedIn
        binding.setting.isGone = !isLoggedIn

        if (isLoggedIn) {
            val headResId = resources.getIdentifier(
                "head${AccountInformation.head_image}",
                "drawable",
                requireContext().packageName
            )
            binding.head.setImageResource(headResId)
            binding.name.text = AccountInformation.name
        }
    }
}